package com.zc.nettycilent._01._1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.*;
import lombok.extern.slf4j.Slf4j;

import java.net.SocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;

@Slf4j
public class TestHttp {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap sb = new ServerBootstrap();
            sb.group(bossGroup, workGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel sc) throws Exception {
                            ChannelPipeline pipeline = sc.pipeline();
                            pipeline.addLast(new HttpServerCodec());
                            pipeline.addLast(new SimpleChannelInboundHandler<HttpObject>() {
                                @Override
                                protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws URISyntaxException {
                                    if (httpObject instanceof HttpRequest request) {
//                                        log.info("接收到了连接:{}", httpObject);
                                        String name = request.method().name();
                                        log.info("请求方式:{}", name);
                                        String path = new URI(request.uri()).getPath();
                                        log.info("请求路径:{}", path);
                                        if(path.equals("/favicon.ico")){
                                            log.info("请求图标");
                                            return;
                                        }
                                        SocketAddress socketAddress = channelHandlerContext.channel().remoteAddress();
                                        log.info("请求地址:{}", socketAddress);
                                        ByteBuf buf = Unpooled.copiedBuffer("hello world", StandardCharsets.UTF_8);
                                        HttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);
                                        httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
                                        httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, buf.readableBytes());
                                        channelHandlerContext.writeAndFlush(httpResponse);
                                    }
                                }
                            });
                        }
                    });
            ChannelFuture channelFuture = sb.bind(1234).sync();
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
}
